package com.unionpay.upyzt.example;

import com.unionpay.upyzt.Upyzt;
import com.unionpay.upyzt.exception.UpyztException;
import com.unionpay.upyzt.model.CombinedGuaranteePayment;
import com.unionpay.upyzt.resp.CombinedGuaranteePaymentResp;
import com.unionpay.upyzt.resp.GuaranteePaymentResp;

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CombinedGuaranteePaymentExample {

    public CombinedGuaranteePaymentExample(String payBalanceAcctId, String recvBalanceAcctId) {
        this.payBalanceAcctId = payBalanceAcctId;
        this.recvBalanceAcctId = recvBalanceAcctId;
    }

    protected String payBalanceAcctId;

    protected String recvBalanceAcctId;

    /**
     * 运行 Demos
     *
     * @param payBalanceAcctId  付款方电子账簿ID
     * @param recvBalanceAcctId 收款方电子账簿ID
     * @return 合并消费担保下单ID
     */
    public static String runDemos(String payBalanceAcctId, String recvBalanceAcctId) throws UpyztException {
        CombinedGuaranteePaymentExample combinedGuaranteePaymentExample = new CombinedGuaranteePaymentExample(payBalanceAcctId, recvBalanceAcctId);

        // 合并消费合并消费担保下单
        CombinedGuaranteePaymentResp combinedGuaranteePaymentResp = combinedGuaranteePaymentExample.create();

        // 合并消费合并消费担保下单订单查询（系统订单号）
        combinedGuaranteePaymentResp = combinedGuaranteePaymentExample.retrieveById(combinedGuaranteePaymentResp.getCombinedGuaranteePaymentId());

        // 合并消费合并消费担保下单订单查询（平台订单号）
        combinedGuaranteePaymentResp = combinedGuaranteePaymentExample.retrieveByOutOrderNo(combinedGuaranteePaymentResp.getOutOrderNo());
        //
        GuaranteePaymentExample guaranteePaymentExample = new GuaranteePaymentExample(payBalanceAcctId, recvBalanceAcctId);
        //
        GuaranteePaymentResp guaranteePaymentResp = guaranteePaymentExample.retrieveById(combinedGuaranteePaymentResp.getGuaranteePaymentResults().get(0).getGuaranteePaymentId());
        //
        guaranteePaymentResp = guaranteePaymentExample.retrieveByOutOrderNo(combinedGuaranteePaymentResp.getGuaranteePaymentResults().get(0).getOutOrderNo());
        return guaranteePaymentResp.getGuaranteePaymentId();
    }

    /**
     * 合并消费担保下单
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public CombinedGuaranteePaymentResp create() throws UpyztException {
        Map<String, Object> params = new HashMap<>();
        params.put("combined_out_order_no", Main.generateRandomOutNo());
        params.put("sent_at", OffsetDateTime.now().toString());
        params.put("pay_balance_acct_id", this.payBalanceAcctId);
        params.put("password", Upyzt.encryptField("pa55w0rd"));
        List<Map<String, Object>> guaranteePaymentParams = new ArrayList<>(1);
        Map<String, Object> guaranteePaymentParam = new HashMap<>();
        guaranteePaymentParam.put("out_order_no", Main.generateRandomOutNo());
        guaranteePaymentParam.put("amount", 20);
        guaranteePaymentParam.put("recv_balance_acct_id", this.recvBalanceAcctId);
        guaranteePaymentParam.put("remark", "用于 SDK 示例测试");
        guaranteePaymentParam.put("metadata", MockData.metadata());
        guaranteePaymentParam.put("extra", MockData.extra());
        guaranteePaymentParams.add(guaranteePaymentParam);
        params.put("guarantee_payment_params", guaranteePaymentParams);
        params.put("remark", "用于 SDK 示例测试");
        params.put("metadata", MockData.metadata());
        params.put("extra", null);
        System.out.println("------- 合并消费担保下单 -------");
        CombinedGuaranteePaymentResp resp = CombinedGuaranteePayment.create(params);
        System.out.println(resp);
        return resp;
    }

    /**
     * 合并消费担保下单订单查询（系统订单号）
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public CombinedGuaranteePaymentResp retrieveById(String id) throws UpyztException {
        System.out.println("------- 合并消费担保下单订单查询（系统订单号） -------");
        CombinedGuaranteePaymentResp resp = CombinedGuaranteePayment.retrieveById(id);
        System.out.println(resp);
        return resp;
    }

    /**
     * 合并消费担保下单订单查询（平台订单号）
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public CombinedGuaranteePaymentResp retrieveByOutOrderNo(String outOrderNo) throws UpyztException {
        System.out.println("------- 合并消费担保下单订单查询（平台订单号） -------");
        CombinedGuaranteePaymentResp resp = CombinedGuaranteePayment.retrieveByOutOrderNo(outOrderNo);
        System.out.println(resp);
        return resp;
    }

}
